package com.itheima.dao;


import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author xiaycbstart
 * @create 2019 12 26 23:07
 */
public interface OrderDao {
    @Insert("insert into t_order values(null,#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @Results({
            @Result(property = "memberId",column = "member_id"),
            @Result(property = "orderDate",column = "orderDate"),
            @Result(property = "orderType",column = "orderType"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "setmealId",column = "setmeal_id"),
    })
    @Options(useGeneratedKeys = true,keyColumn = "id")
    void add(Order order);

    @Select("<script>" +
                "select * from t_order\n" +
                "<where>\n" +
                    "<if test=\"id != null\">\n" +
                        "and id = #{id}\n" +
                    "</if>\n" +
                    "<if test=\"memberId != null\">\n" +
                        "and member_id = #{memberId}\n" +
                    "</if>\n" +
                    "<if test=\"orderDate != null\">\n" +
                        "and orderDate = #{orderDate}\n" +
                    "</if>\n" +
                    "<if test=\"orderType != null\">\n" +
                        "and orderType = #{orderType}\n" +
                    "</if>\n" +
                    "<if test=\"orderStatus != null\">\n" +
                        "and orderStatus = #{orderStatus}\n" +
                    "</if>\n" +
                    "<if test=\"setmealId != null\">\n" +
                        "and setmeal_id = #{setmealId}\n" +
                    "</if>\n" +
                "</where>\n" +
            "</script>")

    List<Order> findByCondition(Order order);
    @Select("select m.name member ,s.name setmeal,o.orderDate orderDate,o.orderType\n" +
            "orderType\n" +
            "from\n" +
            "t_order o,\n" +
            "t_member m,\n" +
            "t_setmeal s\n" +
            "where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    Map findById4Detail(Integer id);
    @Select("select count(id) from t_order where orderDate = #{today}")
    Integer findOrderCountByDate(String today);
    @Select("select count(id) from t_order where orderDate >= #{thisWeekMonday}")
    Integer findOrderCountAfterDate(String thisWeekMonday);
    @Select("select count(id) from t_order where orderDate = #{today} and orderStatus = '已到诊'")
    Integer findVisitsCountByDate(String today);
    @Select("select count(id) from t_order where orderDate >= #{thisWeekMonday}")
    Integer findVisitsCountAfterDate(String thisWeekMonday);
    @Select("select\n" +
            "s.name,\n" +
            "count(o.id) setmeal_count ,\n" +
            "count(o.id)/(select count(id) from t_order) proportion\n" +
            "from t_order o inner join t_setmeal s on s.id = o.setmeal_id\n" +
            "group by o.setmeal_id\n" +
            "order by setmeal_count desc\n" +
            "limit 0,4")
    List<Map> findHotSetmeal();
}
